import sqlite3
from datetime import datetime


class Database:
    def __init__(self):
        with sqlite3.connect('database.db') as db:
            cursor = db.cursor()
            cursor.execute("""CREATE TABLE IF NOT EXISTS payments 
            (id integer primary key, currency text, amount real, payment_time text, description text)""")
            db.commit()

    def add(self, order_id: int, amount: float, currency: str, description: str):
        with sqlite3.connect('database.db') as db:
            db.cursor().execute("INSERT INTO payments VALUES (?, ?, ?, ?, ?)",
                                (order_id,
                                 currency,
                                 amount,
                                 datetime.now().strftime("%d.%m.%Y:%H.%M.%S"),
                                 description))
            db.commit()

    def extract_new_id(self) -> int:
        with sqlite3.connect('database.db') as db:
            cursor = db.cursor()
            # taking last recorded ID in database
            cursor.execute("SELECT * FROM payments WHERE ID = (SELECT MAX(ID) FROM payments)")
            result = cursor.fetchone()
            if result:
                return result[0] + 1
            else:
                # if there is no records, return 100000 as new id :)
                # i think starting from 100 000 better than starting from 0
                return 100000